home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************************************************
-
- UTILInstall.c - A teeny little program for installing UTILs into the LaserWriter Font
- Utility.
-
- Written by Bryan K. Ressler (Beaker), 10/8/91
-
- *****************************************************************************************/
-
- /***************************************************************************************/
- #include "UTILInstall.h"
-
- /***************************************************************************************/
- SFReply gLWFUReply;
- SFReply gUTILReply;
- short gAppRef;
- short gLWFURef;
- short gUTILRef;
- Handle gUTILSpec;
- short gUTILID;
- char gEmptyStr[1] = { 0 };
- DialogPtr gUpdating;
-
- /***************************************************************************************/
- Boolean Init(SysEnvRec *env)
- {
- short err;
-
- InitGraf(&qd.thePort); /* The usual Mac inits */
- InitFonts();
- InitWindows();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- MaxApplZone();
-
- err = SysEnvirons(2,env);
- return(!err && env->systemVersion >= MINSYSTEM);
- }
-
- /***************************************************************************************/
- void ErrorAlert(short strNum)
- {
- Str255 parm;
-
- GetIndString(parm,ERRSTRSID,strNum);
- ParamText(parm,gEmptyStr,gEmptyStr,gEmptyStr);
- StopAlert(ERRALRTID,nil);
- }
-
- /***************************************************************************************/
- Boolean CopyResource(short srcRef,short destRef,ResType type,short id,short idDelta)
- {
- Handle res;
- ResType aType;
- short anID,attribs;
- Str255 name;
-
- UseResFile(srcRef);
- res = Get1Resource(type,id);
- if (!res) return(false);
- GetResInfo(res,&anID,&aType,name);
- attribs = GetResAttrs(res);
- DetachResource(res);
-
- UseResFile(destRef);
- HNoPurge(res);
- AddResource(res,type,id + idDelta,name);
- WriteResource(res);
- SetResAttrs(res,attribs);
-
- return(true);
- }
-
- /***************************************************************************************/
- short CalcResSpace(short UTILID)
- {
- return(UTILRESBASE + (UTILID - UTILIDBASE) * UTILSPACESIZE);
- }
-
- /***************************************************************************************/
- Boolean InstallUTIL(short ID)
- {
- Handle res;
- ResType aType;
- Str255 name;
- ResSpec *specs;
- short anID,numOwned,i,idDelta,attribs,itemsID,idSpace,curSpace;
-
- idSpace = CalcResSpace(ID);
- curSpace = CalcResSpace(gUTILID);
- /* First copy the UTIL itself */
- idDelta = ID - gUTILID;
- if (idDelta) {
- /* Renumbering, so we need to write a new ResSpace into the UTIL */
- UseResFile(gUTILRef);
- res = Get1Resource(UTILTYPE,gUTILID);
- if (!res) return(false);
- GetResInfo(res,&anID,&aType,name);
- attribs = GetResAttrs(res);
- DetachResource(res);
-
- *(short *)(*res + RESSPACEOFFSET) = idSpace;
-
- UseResFile(gLWFURef);
- HNoPurge(res);
- AddResource(res,aType,ID,name);
- WriteResource(res);
- SetResAttrs(res,attribs);
- } else if (!CopyResource(gUTILRef,gLWFURef,UTILTYPE,gUTILID,idDelta))
- return(false);
-
- /* Now loop through the owned resources and copy them */
- idDelta = idSpace - curSpace;
- UseResFile(gUTILRef);
- LoadResource(gUTILSpec);
- MoveHHi(gUTILSpec); HLock(gUTILSpec); HNoPurge(gUTILSpec);
- numOwned = **(short **)gUTILSpec;
- specs = (ResSpec *)(*gUTILSpec + sizeof(short));
- for (i = 0; i < numOwned; i++) {
- /* Copy one owned resource */
- if (idDelta && specs[i].type == ALRTTYPE || specs[i].type == DLOGTYPE) {
- UseResFile(gUTILRef);
- res = Get1Resource(specs[i].type,specs[i].id);
- if (!res) return(false);
- GetResInfo(res,&anID,&aType,name);
- attribs = GetResAttrs(res);
- DetachResource(res);
-
- /* Renumber resource references within ALRTs and DLOGs (ONLY!!!) */
- if (aType == ALRTTYPE) {
- /* Renumber only if it was in our old resource ID space */
- itemsID = (*((AlertTemplate **)res))->itemsID;
- if (itemsID >= curSpace && itemsID < curSpace + UTILSPACESIZE)
- (*((AlertTemplate **)res))->itemsID += idDelta;
- } else {
- /* Renumber only if it was in our old resource ID space */
- itemsID = (*((DialogTemplate **)res))->itemsID;
- if (itemsID >= curSpace && itemsID < curSpace + UTILSPACESIZE)
- (*((DialogTemplate **)res))->itemsID += idDelta;
- }
-
- UseResFile(gLWFURef);
- HNoPurge(res);
- AddResource(res,aType,specs[i].id + idDelta,name);
- WriteResource(res);
- SetResAttrs(res,attribs);
- } else if (!CopyResource(gUTILRef,gLWFURef,specs[i].type,specs[i].id,idDelta))
- return(false);
- }
- HUnlock(gUTILSpec); HPurge(gUTILSpec);
-
- /* Update the LWFU resource file */
- UpdateResFile(gLWFURef);
-
- return(true);
- }
-
- /***************************************************************************************/
- void SuperviseInstall(void)
- {
- Handle existingUTIL,newUTIL;
- short ID,anID,err = noErr;
- ResType aType;
- Str255 name;
- long alertUp;
-
- ShowWindow(gUpdating);
- DrawDialog(gUpdating);
- alertUp = TickCount();
- gLWFURef = OpenRFPerm(gLWFUReply.fName,gUTILReply.vRefNum,fsRdWrPerm);
- if (gLWFURef != -1) {
- UseResFile(gUTILRef);
- newUTIL = Get1Resource(UTILTYPE,gUTILID);
- GetResInfo(newUTIL,&anID,&aType,name);
-
- UseResFile(gLWFURef);
-
- existingUTIL = Get1NamedResource(UTILTYPE,name);
- if (existingUTIL)
- err = ALREADYTHERE;
-
- if (!err) {
- /* Figure what UTIL ID to use */
- ID = gUTILID;
- existingUTIL = Get1Resource(UTILTYPE,ID);
- while (existingUTIL)
- existingUTIL = Get1Resource(UTILTYPE,++ID);
-
- /* ID now holds the first available UTIL ID */
- err = InstallUTIL(ID) ? noErr : BADINSTALL;
- }
-
- /* Close open files */
- CloseResFile(gUTILRef);
- CloseResFile(gLWFURef);
- UseResFile(gAppRef);
-
- /* Put up an alert if necessary */
- if (err)
- ErrorAlert(err);
- else {
- /* Give user reassuring delay */
- if (TickCount() - alertUp < 120)
- Delay(120L,&alertUp);
- NoteAlert(SUCCESSALRTID,nil);
- }
- } else ErrorAlert(NOOPENLWFU);
- }
-
- /***************************************************************************************/
- Boolean CheckUTIL(void)
- {
- ResType type;
- Str255 name;
- Boolean retVal = false;
-
- gAppRef = CurResFile();
- gUTILRef = OpenRFPerm(gUTILReply.fName,gUTILReply.vRefNum,fsRdPerm);
- if (gUTILRef != -1) {
- if (CountResources(UTILSPECTYPE) == 1) {
- /* One UTIL per file, please */
- gUTILSpec = GetIndResource(UTILSPECTYPE,1);
- if (gUTILSpec) {
- GetResInfo(gUTILSpec,&gUTILID,&type,name);
- if (GetResource(UTILTYPE,gUTILID))
- retVal = true;
- else ErrorAlert(NOUTIL);
- } else ErrorAlert(NOUTILSPEC);
- } else ErrorAlert(BADFORMAT);
- } else ErrorAlert(NOOPENUTIL);
-
- /* Leave the file open only if the check was successful */
- if (!retVal)
- CloseResFile(gUTILRef);
-
- return(retVal);
- }
-
- /***************************************************************************************/
- pascal Boolean LWFUFSFilter(HFileParam *pb)
- {
- if (pb->ioFlFndrInfo.fdCreator == LWFUCREATOR && !(pb->ioFlAttrib & BUSYATTRIB))
- return(false);
- else return(true);
- }
-
- /***************************************************************************************/
- Boolean FindFiles(void)
- {
- SFTypeList types;
- Point where;
- DialogPtr locate;
- Str255 desc;
- GrafPtr oldPort;
- Boolean retVal = false;
-
- GetPort(&oldPort);
-
- /* Put up "please locate..." dialog */
- locate = GetNewDialog(LOCATEDLOGID,nil,(WindowPtr)-1);
-
- if (locate) {
- /* Set it up to say "Please locate LWFU" */
- SetPort(locate);
- GetIndString(desc,INSTALLSTRSID,LOCATELWFU);
- ParamText(desc,gEmptyStr,gEmptyStr,gEmptyStr);
-
- /* Show window, note where to put the SF dialog */
- ShowWindow(locate); DrawDialog(locate);
- where.h = locate->portRect.left;
- where.v = locate->portRect.bottom + BOTTOMOFFSET;
- LocalToGlobal(&where);
-
- /* Locate the LaserWriter Font Utility */
- types[0] = 'APPL';
- SFGetFile(where,gEmptyStr,(FileFilterProcPtr)LWFUFSFilter,1,
- types,nil,&gLWFUReply);
-
- /* If they didn't cancel, locate UTIL file */
- if (gLWFUReply.good) {
- /* Set up locate description to say "Please locate UTIL" */
- SetPort(locate);
- GetIndString(desc,INSTALLSTRSID,LOCATEUTIL);
- ParamText(desc,gEmptyStr,gEmptyStr,gEmptyStr);
- DrawDialog(locate);
-
- /* Locate the UTIL file */
- types[0] = 'UTIL';
- SFGetFile(where,gEmptyStr,nil,1,types,nil,&gUTILReply);
- if (gUTILReply.good)
- retVal = true;
- }
-
- DisposDialog(locate);
- }
-
- SetPort(oldPort);
- return(retVal);
- }
-
- /***************************************************************************************/
- void UTILInstall(void)
- {
- if (Alert(ABOUTALRTID,nil) == ok) { /* Put up the spash screen */
- gUpdating = GetNewDialog(UPDATINGDLOGID,nil,(WindowPtr)-1);
- if (FindFiles()) /* Locate LWFU and UTIL file */
- if (CheckUTIL()) /* Check out the UTIL file */
- SuperviseInstall(); /* Install if all is well */
- DisposDialog(gUpdating);
- }
- }
-
- /***************************************************************************************/
- main()
- {
- SysEnvRec env;
-
- if (Init(&env))
- UTILInstall(); /* Knock yourself out! */
- else ErrorAlert(BADENV);
- }
-